class Admins::SptitlesController < Admins::BaseController
  before_action :set_sptitle, only: [:show, :edit, :update, :destroy]

  def index
    @sptitles = Sptitle.all.order("ordinal desc").order("created_at desc").page(params[:page])
  end

  def show
  end

  def new
    if params[:sptitle].blank?
      @sptitle = Sptitle.new
    else
      @sptitle = Sptitle.new(sptitle_params)
    end
  end

  def edit
  end

  def create
    @sptitle = Sptitle.new(sptitle_params)
    if @sptitle.save
        redirect_to :action => :index, notice: '专题创建成功'
    else
        flash[:error] = @sptitle.errors.full_messages.join(",")
        redirect_to :action => :new,:sptitle => @sptitle.attributes
    end
  end

  def update
    if @sptitle.update(sptitle_params)
      flash[:success] = '专题更新成功'
      redirect_to :action => :index
    else
      flash[:error] = @sptitle.errors.full_messages.join(",")
      render :edit
    end
  end

  def destroy
    @sptitle.destroy
    flash[:success] = '专题删除成功'
    redirect_to :action => :index
  end

  private
    def set_sptitle
      @sptitle = Sptitle.find(params[:id])
    end

    def sptitle_params
      params.require(:sptitle).permit(:name,:picture)
    end
end